<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch, computed } from "vue";
import { useSelectState } from "../composables/useSelectState";

const { setActiveSelect, isSelectActive } = useSelectState();
const selectId = ref(Math.random().toString(36).substr(2, 9));
const emits = defineEmits(["selectConfirm", "clearSetting"]);

const props = defineProps({
  selectLabel: {
    type: String,
    default: "",
  },
  showBtns: {
    type: Boolean,
    default: true,
  },
});

// 使用计算属性来控制显示状态
const showSelectModel = computed(() => isSelectActive(selectId.value));

const selectChange = () => {
  if (showSelectModel.value) {
    setActiveSelect(null);
  } else {
    setActiveSelect(selectId.value);
  }
};

const selectConfirm = () => {
  emits("selectConfirm");
  setActiveSelect(null);
};

const clearSetting = () => {
  emits("clearSetting");
};

const handleClickOutside = (event: MouseEvent) => {
  const target = event.target as HTMLElement;
  if (!target.closest(".select")) {
    setActiveSelect(null);
  }
};

onMounted(() => {
  document.addEventListener("click", handleClickOutside);
});

onUnmounted(() => {
  document.removeEventListener("click", handleClickOutside);
  // 组件卸载时，如果当前组件是激活的，则清除激活状态
  if (isSelectActive(selectId.value)) {
    setActiveSelect(null);
  }
});
</script>

<template>
  <div class="select">
    <div class="filter-select" @click.stop="selectChange">
      <span>{{ props.selectLabel }}</span>
      <img src="~/assets/select-down.png" alt="" />
    </div>
    <div class="select-content" v-show="showSelectModel">
      <div class="slot" @click.stop>
        <slot></slot>
      </div>
      <div v-if="showBtns" class="footer-btn" @click.stop>
        <span class="clear" @click="clearSetting">清除所有</span>
        <span class="confirm" @click="selectConfirm">确认选择</span>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.select {
  position: relative;
}
.filter-select {
  padding: 10px 20px;
  border-radius: 8px;
  border: 1px solid rgba(25, 30, 40, 0.08);
  display: flex;
  align-items: center;
  cursor: pointer;

  span {
    color: #191e28;
  }
  img {
    width: 16px;
    height: 16px;
    margin-left: 6px;
  }
}
.select-content {
  background: #fff;
  border-radius: 16px;
  margin-top: 8px;
  border: 1px solid rgba(25, 30, 40, 0.12);
  box-shadow: 0 0 20px 0 rgba(11, 13, 18, 0.06);
  padding: 20px 24px 0;
  position: absolute;
  top: 40px;
  left: 0;
  z-index: 2;
  .footer-btn {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-top: 1px solid rgba(25, 30, 40, 0.12);
    margin: 0 -24px;
    padding: 12px 24px;
    box-sizing: border-box;
    .clear {
      display: inline-block;
      white-space: nowrap;
      color: #1dbf73;
      cursor: pointer;
    }
    .confirm {
      width: 96px;
      height: 40px;
      background: #1dbf73;
      border-radius: 8px;
      color: #fff;
      text-align: center;
      line-height: 40px;
    }
  }
}
.slot {
  max-height: 500px;
  overflow-y: auto;
}
</style>
